using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WCFServer.Client
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            Server.ServerClient client = new WCFServer.Client.Server.ServerClient();
            Server.ServerClient client2 = new WCFServer.Client.Server.ServerClient();
            try
            {
                this.textBox1.Text += System.Environment.NewLine + "Openning client";
                client.Open();
                this.textBox1.Text += System.Environment.NewLine + "Getting details of user karpik...";
                Server.Player player = client.GetPlayerDetails("karpik");
                this.textBox1.Text += System.Environment.NewLine + player.imie + " " + player.nazwisko + " from " + player.miasto + " and his sex is " + player.plec;

                this.textBox1.Text += System.Environment.NewLine + "Adding player to DB";
                client.CreatePlayer(player);

                this.textBox1.Text += System.Environment.NewLine + "karpik Trying to join game...";
                Server.JoinResultJoinState state = client.JoinGame("karpik");
                this.textBox1.Text += System.Environment.NewLine + "karpik Joined game, status: " + state.ToString();

                client.Close();
                client = new WCFServer.Client.Server.ServerClient();
                client.Open();

                {
                    client2.Open();
                    this.textBox1.Text += System.Environment.NewLine + "sopel Trying to join game...";
                Server.JoinResultJoinState state2 = client2.JoinGame("Sopel");
                this.textBox1.Text += System.Environment.NewLine + "sopel Joined game, status: " + state2.ToString();
                    
                    Server.Karta card2 = new WCFServer.Client.Server.Karta();
                card2.kolor = WCFServer.Client.Server.Cardcolor.Clubs;
                card2.wartosc = WCFServer.Client.Server.CardCardFace.ace;
                    Server.PlayerResultPlayerState playerstate2;

                    this.textBox1.Text += System.Environment.NewLine + "sopel Placing card...";
                    Server.GameResultGameState gamestate2 = client2.UserPlaceCard(card2, "Sopel", out playerstate2);
                    this.textBox1.Text += System.Environment.NewLine + "sopel Placed card, gamestate: " + gamestate2.ToString() + " ,playerstate: " + playerstate2.ToString(); 
                }
                client2.Close();

                Server.Karta card = new WCFServer.Client.Server.Karta();
                card.kolor = WCFServer.Client.Server.Cardcolor.Spades;
                card.wartosc = WCFServer.Client.Server.CardCardFace.nine;
                Server.PlayerResultPlayerState playerstate;

                this.textBox1.Text += System.Environment.NewLine + "karpik Placing card...";
                Server.GameResultGameState gamestate = client.UserPlaceCard(card, "karpik", out playerstate);
                this.textBox1.Text += System.Environment.NewLine + "karpik Placed card, gamestate: " + gamestate.ToString() + " ,playerstate: " + playerstate.ToString(); ;

                this.textBox1.Text += System.Environment.NewLine + "---------------------------------";
                this.textBox1.Text += System.Environment.NewLine + "THE WINNER IS : " + client.GetWinner();


                this.textBox1.Text += System.Environment.NewLine + "karpik Leaving game...";
                Server.LeaveResultLeveState leavestate = client.LeaveGame("karpik");
                this.textBox1.Text += System.Environment.NewLine + "karpik Left Game, leavestate: " + leavestate.ToString(); ;
                client.Close();
                this.textBox1.Text += System.Environment.NewLine + "Client closed";
            }
            catch (Exception ex)
            {
                this.textBox1.Text += System.Environment.NewLine + ex.Message;
            }
            finally
            {
                if (client != null && client.State == System.ServiceModel.CommunicationState.Opened)
                {
                    client.Close();
                }
            }
        }
    }
}